package web

//
import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
)

// DeviceController 门禁设备管理
type DeviceController struct {
	EntranceEquipmentBll *bll.EEBll `inject:""`
}

// QueryPage 查询分页数据
func (a *DeviceController) QueryPage(ctx *context.Context) error {
	ctx.WriteOperateLog("DeviceController.Create", "查询门禁设备信息列表分页数据")
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()
	params := schema.EntranceEquipment{
		EquipmentID:   ctx.Query("equipment_id"),
		EquipmentName: ctx.Query("equipment_name"),
		EquipmentType: ctx.Query("equipment_type"),
		GuardZoneType: ctx.Query("guard_zone_type"),
		//GuardZone:     ctx.Query("guard_zone"),
		Memo: ctx.Query("note"),
	}

	total, items, err := a.EntranceEquipmentBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// GetDetailCard 门禁设备精确查找获取门禁设备信息
func (a *DeviceController) GetDetailCard(ctx *context.Context) error {
	ctx.WriteOperateLog("DeviceController.Create", "查询门禁设备信息详情")
	param := ctx.Param("id")
	fmt.Println(param)
	result, err := a.EntranceEquipmentBll.GetDetailCard(param)

	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(result)
}

// Create 添加创建门禁设备
func (a *DeviceController) Create(ctx *context.Context) error {
	//记录操作日志
	ctx.WriteOperateLog("DeviceController.Create", "增加门禁设备信息")
	var item schema.EntranceEquipment
	//这里的DecodeJSON方法不校验json数据，不同于ParseJSON方法会校验数据。
	if err := ctx.DecodeJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}
	item.Creator = ctx.GetUserID()
	err := a.EntranceEquipmentBll.Create(&item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Update 修改更新门禁设备信息
func (a *DeviceController) Update(ctx *context.Context) error {
	return nil
}

// Delete 删除门禁设备信息
func (a *DeviceController) Delete(ctx *context.Context) error {
	ctx.WriteOperateLog("DeviceController.Delete", "删除设备节点信息")
	param := ctx.Param("id")
	err := a.EntranceEquipmentBll.Delete(param)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// QueryTreeNode 查询建筑树形结构数据
func (a *DeviceController) QueryTreeNode(ctx *context.Context) error {
	ctx.WriteOperateLog("DeviceController.QueryTreeNode", "获取建筑节点树")
	items, err := a.EntranceEquipmentBll.QueryTreeNode()
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}

// GetEquipmentInfo 查询直接上级设备清单
func (a *DeviceController) GetEquipmentInfo(ctx *context.Context) error {
	ctx.WriteOperateLog("DeviceController.GetEquipmentInfo", "获取建筑节点树")
	items, err := a.EntranceEquipmentBll.GetEquipmentInfo(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}
